{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#导入数据+把0/1型字符数据转换为数值\n",
    "data_path='data.xlsx'\n",
    "data=pd.read_excel(io=data_path,encoding='gbk')\n",
    "def transform(row):\n",
    "    if row['是否续保']=='是':\n",
    "        row['是否续保']='1'\n",
    "    else:\n",
    "        row['是否续保']='0'\n",
    "    return row\n",
    "def transform2(row):\n",
    "    if row['客户类别']=='个人':\n",
    "        row['客户类别']='1'\n",
    "    else:\n",
    "        row['客户类别']='0'\n",
    "    return row\n",
    "def transform3(row):\n",
    "    if row['是否投保车损']=='投保车损':\n",
    "        row['是否投保车损']='1'\n",
    "    else:\n",
    "        row['是否投保车损']='0'\n",
    "    return row\n",
    "def transform4(row):\n",
    "    if row['是否投保盗抢']=='投保车损':\n",
    "        row['是否投保盗抢']='1'\n",
    "    else:\n",
    "        row['是否投保盗抢']='0'\n",
    "    return row\n",
    "def transform5(row):\n",
    "    if row['是否投保车上人员']=='投保车损':\n",
    "        row['是否投保车上人员']='1'\n",
    "    else:\n",
    "        row['是否投保车上人员']='0'\n",
    "    return row\n",
    "def transform6(row):\n",
    "    if row['保单性质']=='转保':\n",
    "        row['保单性质']='1'\n",
    "    else:\n",
    "        row['保单性质']='0'\n",
    "    return row\n",
    "def transform7(row):\n",
    "    if row['客户类别']=='个人':\n",
    "        row['客户类别']='1'\n",
    "    else:\n",
    "        row['客户类别']='0'\n",
    "    return row\n",
    "def transform8(row):\n",
    "    if row['险种']=='交强险':\n",
    "        row['险种']='1'\n",
    "    else:\n",
    "        row['险种']='0'\n",
    "    return row\n",
    "\n",
    "\n",
    "\n",
    "data=data.apply(transform,axis=1)\n",
    "data['是否续保'] = data['是否续保'].astype(np.float64)\n",
    "data=data.apply(transform3,axis=1)\n",
    "data['是否投保车损'] = data['是否投保车损'].astype(np.float64)\n",
    "data=data.apply(transform4,axis=1)\n",
    "data['是否投保盗抢'] = data['是否投保盗抢'].astype(np.float64)\n",
    "data=data.apply(transform5,axis=1)\n",
    "data['是否投保车上人员'] = data['是否投保车上人员'].astype(np.float64)\n",
    "data=data.apply(transform6,axis=1)\n",
    "data['保单性质'] = data['保单性质'].astype(np.float64)\n",
    "data=data.apply(transform7,axis=1)\n",
    "data['客户类别'] = data['客户类别'].astype(np.float64)\n",
    "data=data.apply(transform8,axis=1)\n",
    "data['险种'] = data['险种'].astype(np.float64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#删除部分数据\n",
    "data=data.drop(['品牌'],axis=1)\n",
    "data=data.drop(['车系'],axis=1)\n",
    "data=data.drop(['起保日期'],axis=1)\n",
    "data=data.drop(['终止日期'],axis=1)\n",
    "data=data.drop(['保单号'],axis=1)\n",
    "data=data[~data['NCD'].isin(['新保无记录'])]\n",
    "data=data[~data['车辆用途'].isin(['重、中型载货汽车'])]\n",
    "data=data[~data['车辆用途'].isin(['其他汽车'])]\n",
    "data=data[~data['车辆种类'].isin(['特种二挂车'])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#将部分数据NAN填充\n",
    "data['被保险人性别']=data['被保险人性别'].fillna('N')\n",
    "data['已决赔款']=data['已决赔款'].fillna(0)\n",
    "data['三者险保额']=data['三者险保额'].fillna(0)\n",
    "data=data.fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#利用onehot把值转换为数字类型\n",
    "data_dummies1=pd.get_dummies(data['车辆种类'])\n",
    "data_dummies2=pd.get_dummies(data['渠道'])\n",
    "data_dummies3=pd.get_dummies(data['投保类别'])\n",
    "data_dummies4=pd.get_dummies(data['车辆用途'])\n",
    "data_dummies5=pd.get_dummies(data['是否本省车牌'])\n",
    "data_dummies6=pd.get_dummies(data['NCD'])\n",
    "data_dummies7=pd.get_dummies(data['风险类别（A最低，E最高）'])\n",
    "data_dummies8=pd.get_dummies(data['使用性质'])\n",
    "data_dummies9=pd.get_dummies(data['被保险人性别'])\n",
    "\n",
    "data=data.drop(['车辆种类'],axis=1)\n",
    "data=data.drop(['渠道'],axis=1)\n",
    "data=data.drop(['投保类别'],axis=1)\n",
    "data=data.drop(['车辆用途'],axis=1)\n",
    "data=data.drop(['是否本省车牌'],axis=1)\n",
    "data=data.drop(['NCD'],axis=1)\n",
    "data=data.drop(['风险类别（A最低，E最高）'],axis=1)\n",
    "data=data.drop(['使用性质'],axis=1)\n",
    "data=data.drop(['客户类别'],axis=1)\n",
    "data=data.drop(['被保险人性别'],axis=1)\n",
    "\n",
    "data=pd.concat([data,data_dummies1],axis=1) \n",
    "data=pd.concat([data,data_dummies2],axis=1) \n",
    "data=pd.concat([data,data_dummies3],axis=1) \n",
    "data=pd.concat([data,data_dummies4],axis=1) \n",
    "data=pd.concat([data,data_dummies5],axis=1) \n",
    "data=pd.concat([data,data_dummies6],axis=1) \n",
    "data=pd.concat([data,data_dummies7],axis=1) \n",
    "data=pd.concat([data,data_dummies8],axis=1) \n",
    "data=pd.concat([data,data_dummies9],axis=1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result=np.array(data['是否续保'].values)\n",
    "data=data.drop(['是否续保'],axis=1)\n",
    "predict=np.array(data.values)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 建立逻辑回归模型并进行交叉验证"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\buttle\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\linear_model\\logistic.py:433: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.\n",
      "  FutureWarning)\n",
      "c:\\users\\buttle\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\linear_model\\logistic.py:433: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.\n",
      "  FutureWarning)\n",
      "c:\\users\\buttle\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\linear_model\\logistic.py:433: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.\n",
      "  FutureWarning)\n",
      "c:\\users\\buttle\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\linear_model\\logistic.py:433: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.\n",
      "  FutureWarning)\n",
      "c:\\users\\buttle\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\linear_model\\logistic.py:433: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.\n",
      "  FutureWarning)\n",
      "c:\\users\\buttle\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\linear_model\\logistic.py:433: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.\n",
      "  FutureWarning)\n",
      "c:\\users\\buttle\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\linear_model\\logistic.py:433: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.\n",
      "  FutureWarning)\n",
      "c:\\users\\buttle\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\linear_model\\logistic.py:433: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.\n",
      "  FutureWarning)\n",
      "c:\\users\\buttle\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\linear_model\\logistic.py:433: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.\n",
      "  FutureWarning)\n",
      "c:\\users\\buttle\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\linear_model\\logistic.py:433: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.\n",
      "  FutureWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "交叉验证结果: [0.99526934 0.99649016 0.99633756 0.99557455 0.99587975 0.99587912\n",
      " 0.996337   0.996337   0.99679438 0.99679438]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "lr = LogisticRegression(max_iter=100000,tol=1e-7)\n",
    "import sklearn.model_selection as sk_model_selection\n",
    "accs=sk_model_selection.cross_val_score(lr,predict, y=result, scoring=None,cv=10, n_jobs=1)\n",
    "print('交叉验证结果:',accs)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 保存和提取模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['logistic_lr.model']"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.externals import joblib\n",
    "joblib.dump(lr,\"logistic_lr.model\")  \n",
    "joblib.load(\"logistic_lr.model\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### SVM建立"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 留出法数据\n",
    "predict1=predict[:50000]\n",
    "predict2=predict[50000:-1]\n",
    "result1=result[:50000]\n",
    "result2=result[50000:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.svm import SVC\n",
    "clf=SVC(probability=True)\n",
    "clf.fit(predict1,result1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 前馈神经网络模型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 数据归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.to_csv('temp.csv')\n",
    "data=pd.read_csv('temp.csv',index_col=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "#数据归一化\n",
    "def zero_one1(row):\n",
    "    row['三者险保额']=row['三者险保额']/2000000\n",
    "    return row\n",
    "def zero_one2(row):\n",
    "    row['签单保费']=row['签单保费']/38297\n",
    "    return row\n",
    "def zero_one3(row):\n",
    "    row['立案件数']=row['立案件数']/2000000\n",
    "    return row\n",
    "def zero_one4(row):\n",
    "    row['已决赔款']=row['已决赔款']/983620.00\n",
    "    return row\n",
    "def zero_one5(row):\n",
    "    row['新车购置价']=row['新车购置价']/5100000\n",
    "    return row\n",
    "def zero_one6(row):\n",
    "    row['车龄']=row['车龄']/24\n",
    "    return row\n",
    "def zero_one7(row):\n",
    "    row['被保险人年龄']=row['被保险人年龄']/75\n",
    "    return row\n",
    "def zero_one8(row):\n",
    "    row['续保年']=row['续保年']/8\n",
    "    return row\n",
    "\n",
    "data=data.apply(zero_one1,axis=1)\n",
    "data=data.apply(zero_one2,axis=1)\n",
    "data=data.apply(zero_one3,axis=1)\n",
    "data=data.apply(zero_one4,axis=1)\n",
    "data=data.apply(zero_one5,axis=1)\n",
    "data=data.apply(zero_one6,axis=1)\n",
    "data=data.apply(zero_one7,axis=1)\n",
    "data=data.apply(zero_one8,axis=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保存归一化数据\n",
    "data.to_csv('归一化第一版.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#训练神经网络\n",
    "import sklearn.neural_network as sk_nn\n",
    "model = sk_nn.MLPClassifier(activation='relu',solver='adam',alpha=0.0001,learning_rate='adaptive',learning_rate_init=0.01,max_iter=2000)\n",
    "model.fit(predict1,result1)\n",
    "acc=model.score(predict2,result2) #根据给定数据与标签返回正确率的均值\n",
    "print('神经网络模型评价:',acc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
